﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Gift.DAL.crm;
using Gift.Model.crm;
using Gift.Model.crm.eNum;

namespace WebSite.crm.order
{
    public partial class goodsIn : BaseCrm
    {
        readonly t_DALGoods theGoods = new t_DALGoods();
        readonly t_DALBrand theBrand = new t_DALBrand();


        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                base.ValidateLoginRule(eRule.GoodsIn);
                this.BindGoodsTree();
           

            }
        }


        void BindGoodsTree()
        {
            DataTable tbBrand = theBrand.GetListNoDeleted("").Tables[0];
            DataTable tbGoods = theGoods.GetListNoDeleted("").Tables[0];
            DataView dvGoods;
            foreach (DataRow dr in tbBrand.Rows)
            {
                tbGoods.DefaultView.RowFilter = string.Format("brandId = {0}", dr["brandId"].ToString());
                dvGoods = tbGoods.DefaultView;
                BindTreeNode(dr["brandId"].ToString(), dr["brandName"].ToString(), dr["brandId"].ToString(), dr["brandName"].ToString(), 1);
                for (int i = 0; i < dvGoods.Count; i++)
                {
                    BindTreeNode(dvGoods[i]["brandId"].ToString(), dvGoods[i]["brandName"].ToString(), dvGoods[i]["goodsId"].ToString(), dvGoods[i]["goodsName"].ToString(), 2);
                }
 
            }

        }

        void BindTreeNode(string parentId, string parentName, string subId, string subName, int level)
        {
            ExtAspNet.TreeNode node;
            int nodeIndex = FindNodeById(parentId);
            if (nodeIndex < 0)
            {
                node = new ExtAspNet.TreeNode();
                node.NodeID = parentId;
                node.SingleClickExpand = true;
                node.Text = parentName;
                node.Target = "main";
                if (level == 1)
                {
                    nodeIndex = FindNodeById(parentId);
                    this.treeGoods.Nodes.Add(node);

                }
            }

            if (this.treeGoods.FindNode(parentId) != null && level == 2)
            {
                node = new ExtAspNet.TreeNode();
                node.NodeID = subId;
                node.SingleClickExpand = true;
                node.Target = "main";
                node.NavigateUrl = "goodsInDetail.aspx?gid=" + subId;
                node.Text = subName;
                this.treeGoods.FindNode(parentId).Nodes.Add(node);
            }
    

        }

        int FindNodeById(string sNodeId)
        {
            for (int i = 0; i < this.treeGoods.Nodes.Count; i++)
            {
                if (this.treeGoods.Nodes[i].NodeID == sNodeId)
                {
                    return i;
                }
            }
            return -1;

        }



   
    }
}